package cn.chengpengper.im.platform.user.interfaces.controller;

import cn.chengpengper.im.platform.common.model.dto.LoginDto;
import cn.chengpengper.im.platform.common.model.dto.RegisterDto;
import cn.chengpengper.im.platform.common.model.vo.LoginVo;
import cn.chengpengper.im.platform.common.response.ResponseMessage;
import cn.chengpengper.im.platform.common.response.ResponseMessageFactory;
import cn.chengpengper.im.platform.user.application.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api("用户登录和注册")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/login")
    public ResponseMessage<LoginVo> login(@Valid @RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto);
        return ResponseMessageFactory.getSuccessResponseMessage(loginVo);
    }

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @PostMapping("/register")
    public ResponseMessage<String> register(@Valid @RequestBody RegisterDto registerDto) {
        userService.register(registerDto);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }
}
